<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-carousel class="caro" autoplay arrows>
        <div slot="prevArrow" class="custom-slick-arrow" style="left: 10px;zIndex: 1"><a-icon type="left-circle" /></div>
        <div slot="nextArrow" class="custom-slick-arrow" style="right: 10px"><a-icon type="right-circle" /></div>
        <img v-for="(item,index) in imgList" :key="index" :src="imageUrl+item.fileName" style="width: 500px;height: 125px;" />
      </a-carousel>
    </div>
    <div class="table-operator">
      <a-button v-if="addEnable" type="primary" icon="plus" @click="$refs.modal.add()">新建</a-button>
      <a-dropdown v-if="removeEnable && selectedRowKeys.length > 0">
        <a-popconfirm
          title="确定删除吗?"
          ok-text="是"
          cancel-text="否"
          @confirm="delByIds(selectedRowKeys)"
        >
          <a-button type="danger" icon="delete" >删除</a-button>
        </a-popconfirm>

      </a-dropdown>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="id"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :columns="columns"
      :data="loadData">
      <span slot="file" slot-scope="text">
        <img v-if="text" :src="imageUrl + text" style="width: 150px;height: 55px;" />
        <span v-else>无图片</span>
      </span>
      <span slot="open" slot-scope="text"><a-switch disabled checked-children="开" un-checked-children="关" :checked="text" /></span>
      <span slot="action" slot-scope="text, record">
        <a v-if="editEnabel" @click="handleEdit(record)">编辑</a>
        <a-divider type="vertical" />
        <a-popconfirm
          title="确定删除吗?"
          ok-text="是"
          cancel-text="否"
          @confirm="delByIds([record.id])"
        >
          <a v-if="removeEnable" >删除</a>
        </a-popconfirm>

      </span>
    </s-table>
    <appBanner-modal ref="modal" @ok="handleOk" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { getSysAppBannerList, getAppBannerList, delSysAppBanner } from '@/api/appBanner'
import AppBannerModal from './modules/AppBannerModal.vue'
import { checkPermission } from '@/utils/permissions'

export default {
  name: 'TableList',
  components: {
    STable,
    AppBannerModal
  },
  data () {
    return {
      imageUrl: process.env.VUE_APP_API_BASE_URL + '/dfs/img?fileName=',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        // {
        //   title: '链接地址',
        //   dataIndex: 'href'
        // },
        {
          title: '图片',
          dataIndex: 'fileName',
          scopedSlots: { customRender: 'file' }
        },
        {
          title: '显示顺序',
          dataIndex: 'seq'
        },
        {
          title: '是否显示',
          dataIndex: 'open',
          scopedSlots: { customRender: 'open' }
        },
        {
          title: '操作',
          width: '200px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getSysAppBannerList(Object.assign(parameter, this.queryParam))
      },
      selectedRowKeys: [],
      selectedRows: [],
      addEnable: checkPermission('system:banner:add'),
      editEnabel: checkPermission('system:banner:edit'),
      removeEnable: checkPermission('system:banner:remove'),
      imgList: []
    }
  },
  filters: {},
  created () {
    getAppBannerList().then(res => {
      console.log(res)
      if (res.code === 0) {
        this.imgList = res.data
      }
    })
  },
  methods: {
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleAdd () {
      this.$refs.modal.add()
    },
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh(true)
      console.log('handleSaveOk')
    },
    delByIds (ids) {
      delSysAppBanner({ ids: ids.join(',') }).then(res => {
        if (res.code === 0) {
          this.$message.success('删除成功')
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
        this.selectedRowKeys = []
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
<style scoped>
/* For demo */
.caro{
  width: 500px;
  height: 125px;
  margin: 10px auto;
}
.ant-carousel >>> .slick-slide {
  text-align: center;
  height: 125px;
  line-height: 125px;
  background: #364d79;
  overflow: hidden;
}

.ant-carousel >>> .custom-slick-arrow {
  width: 25px;
  height: 25px;
  font-size: 25px;
  color: #fff;
  background-color: rgba(31, 45, 61, 0.11);
  opacity: 0.3;
}
.ant-carousel >>> .custom-slick-arrow:before {
  display: none;
}
.ant-carousel >>> .custom-slick-arrow:hover {
  opacity: 0.5;
}

.ant-carousel >>> .slick-slide h3 {
  color: #fff;
}
</style>
